/**
 * Created by mkDark on 2018/3/20.
 */
var moment = require('moment');
var async = require('async');
var superagent = require('superagent');



var thirdUtil = (function () {

    function thirdUtil() {

    }

    /**
     * 验证居民身份证
     *
     * @author 王俊
     * @date 2018-06-25
     *
     * @param {Object} options 参数
     * @param {String} options.name 身份证名称
     * @param {String} options.idCardNum 身份证号码
     * @param {Function} callback 回调函数
     *                      err:
     *                      data: {
     *                              "idCard": "511126199511111111",
     *                              "name": "张三",
     *                              "sex": "男",
     *                              "area": "四川省乐山市夹江县",
     *                              "province": "四川省",
     *                              "city": "乐山市",
     *                              "prefecture": "夹江县",
     *                              "birthday": "1995-11-11",
     *                              "addrCode": "511126",
     *                              "lastCode": "1"
     *                              }
     */
    thirdUtil.checkIdCard = function (options, callback) {

        var url = 'https://idcert.market.alicloudapi.com/idcard';
        url += '?idCard='+ options.idCardNum;
        url += '&name=' + options.name;
        url = encodeURI(url);
        superagent.get(url)
            .set('Authorization', 'APPCODE e8feb2acc90048e792e951eaff2dd9cf')
            .end(function (error, response) {
                if(error) {
                    return callback(error, null);
                }
                var retData = null;
                try {
                    retData = JSON.parse(response.text);
                } catch (e) {
                    return callback(e, null);
                }
                if(!retData) {
                    return callback('查询失败', null);
                }
                if(retData.status === '01') {
                    return callback(null, retData);
                }
                return callback(retData.msg, null);
            });
    }


    return thirdUtil;
})();

module.exports = thirdUtil;